把数组排成最小的数

把数组排成最小的数

题目描述

输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。

  • 将数字拼接成一个数,需要数字越大越排在低位,越小的数字排在高位,所以需要321的1排在尽量高的位置。
1
2
3
4
5
6
7
8
9
10
11
12
function PrintMinNumber(numbers)
{
// write code here
numbers.sort(function(s1,s2){
const n1=`${s1}${s2}`
const n2=`${s2}${s1}`
return n1>n2
})
let min=''
numbers.forEach((i)=>min+=i);
return min;
}